package com.gxa.jd.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.entity.PageResult;
import com.gxa.jd.mapper.GoodsOrderItemMapper;
import com.gxa.jd.pojo.GoodsOrderItem;
import com.gxa.jd.service.GoodsOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GoodsOrderItemServiceImpl implements GoodsOrderItemService {

    @Autowired
    private GoodsOrderItemMapper goodsOrderItemMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<GoodsOrderItem> findAll() {
        return goodsOrderItemMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<GoodsOrderItem> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<GoodsOrderItem> orderItems = (Page<GoodsOrderItem>) goodsOrderItemMapper.selectAll();
        return new PageResult<GoodsOrderItem>(orderItems.getTotal(),orderItems.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<GoodsOrderItem> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return goodsOrderItemMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<GoodsOrderItem> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<GoodsOrderItem> orderItems = (Page<GoodsOrderItem>) goodsOrderItemMapper.selectByExample(example);
        return new PageResult<GoodsOrderItem>(orderItems.getTotal(),orderItems.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public GoodsOrderItem findById(Long id) {
        return goodsOrderItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param
     */
    public void add(GoodsOrderItem goodsOrderItem) {
        goodsOrderItemMapper.insert(goodsOrderItem);
    }

    /**
     * 修改
     * @param
     */
    public void update(GoodsOrderItem goodsOrderItem) {
        goodsOrderItemMapper.updateByPrimaryKeySelective(goodsOrderItem);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        goodsOrderItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(GoodsOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // image
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
            }
            // name
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // order_id
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andLike("orderId","%"+searchMap.get("orderId")+"%");
            }
            // sku_id
            if(searchMap.get("skuId")!=null && !"".equals(searchMap.get("skuId"))){
                criteria.andLike("skuId","%"+searchMap.get("skuId")+"%");
            }

            // num
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }

        }
        return example;
    }

}
